<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="servlets.message.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<%@ page import="hibernate.method.UserMethods"%>

<script> 
$(".receiver").click(function(e){
	 e.preventDefault();
	 var clickedID = $(this).val();
	  $.ajax({
	       url: "../UserDetails",
	       type: "POST",//type of posting the data
	       data: {
		       userID: clickedID
	       },
	       dataType: 'json',
	       success: function (json) {
			var data = json;
			$("#frnd").hide().fadeIn(1000).html("<table>");
		    $("#frnd").append("<tr><td>Nick:</td><td>"+data.username+"</td></tr>");
		    $("#frnd").append("<tr><td>Data urodzenia:</td><td>"+data.birthdate+"</td></tr>");
		    $("#frnd").append("<tr><td>Adres e-mail:</td><td>"+data.email+"</td></tr>");
		    if (data.gender == "male") gender="mezczyzna"; else gender = "kobieta";
		    $("#frnd").append("<tr><td>Z nami od:</td><td>"+data.add_date+"</td></tr>");
		    $("#frnd").append("<tr><td>Plec:</td><td>"+gender+"</td></tr>");
		    $("#frnd").append("</table>");
	       },
	       error: function(xhr, ajaxOptions, thrownError){
	       },
	       timeout : 15000
	  });

	});

</script>
<div class="page-header">
	<h2 class="page-title">Zaproszenia</h2>
</div>

<br />

<div id="content">
<%
						String type = "client";
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						InvitationMethods inv_m = new InvitationMethods();
						 Integer user_id = (Integer) session.getAttribute("user_id");
							UserMethods u_m = new UserMethods();
							List<User> ulist = u_m.list_by_type(sessionFactory, s, type);
						List<Invitation> invlist_rec = inv_m.listActiveInvitationsByReceiver(sessionFactory, s, user_id);
						List<Invitation> invlist_send = inv_m.listActiveInvitationsBySender(sessionFactory, s, user_id);
						
					%> 
					
					<div class="page-header">
<h2 class="page-title">Otrzymane</h2>
</div>

		
		
		<%
		if (invlist_rec.size() > 0) {
			%>
			<form action="../InvitationAccept" method="POST" name='form1'>
			<table>
<tr>
<td>Check</td>
<td>Od</td>
</tr>

			<%
	for (Invitation inv1 : invlist_rec) {
		int inviting = inv1.getInviting();
		User sender = u_m.id_to_user(sessionFactory, s, inviting);
		String sender_nick = sender.getUsername();
		%>
					
<tr>
<td>
<%out.println("<input type=\"radio\" name=\"check\" value=\"" +inv1.getInvitation_id() +"\"" );
%>
</td>
<td><% out.println(sender_nick); %></td>
</tr>		

<%
		} %>
				<tr>
		<td></td>
		<td>
		
<input type="submit" class="mainBtn" value="Zaakceptuj" />
</td>
</table>
</form>
<%
		
		} else if (invlist_rec.size() == 0) out.println("Nie otzymales zadnych nowych zaproszen");
		
		%>

					<div class="page-header">
<h2 class="page-title">Wyslane</h2>
</div>

<%
		if (invlist_send.size() > 0) {
		%>
		<form action="../InvitationDelete" method="POST" name='form2'>
							<table>
<tr>
<td>Check</td>
<td>Od</td>
</tr>
<%
	for (Invitation inv1 : invlist_send) {
		int invited = inv1.getInvited();
		
		User sender = u_m.id_to_user(sessionFactory, s, invited);
		String sender_nick = sender.getUsername();
		%>
		
<tr>
<td>
<%out.println("<input type=\"radio\" name=\"check\" value=\"" +inv1.getInvitation_id() +"\"" );
%>
</td>
<td><% out.println(sender_nick); %></td>
</tr>		
		
<%
		}%>
		<tr>
		<td></td>
		<td>
<input type="submit" class="mainBtn" value="Usuń" />
</td>
</table>
</form>
<%
		} else if (invlist_send.size() == 0) out.println("Nie masz zadnych niezaakceptowanych wyslanych zaproszen");
		%>


	<div class="page-header">
<h2 class="page-title">Wyslij zaproszenie</h2>
</div>


				<form action="../InvitationSend" method="POST" name='formularz'>
				
					<input type="hidden" name = "sender_id" id="sender_id" value="<%=user_id %>"/>
		<table>
			<tr>
				<td>Wyślij zaproszenie do użytkownika:</td>
				<td>
				<select id="receiver" name="receiver" class="receiver">
						<option value="0" selected>Wybierz użytkownika</option>
						<%		if (ulist.size() > 0) {
							for (User u1 : ulist) {
								if (u1==u_m.id_to_user(sessionFactory, s, user_id)) 
								{
									int toRemove=ulist.indexOf(u1);
									System.out.println(toRemove);
								//	ulist.remove(toRemove);
								}
								else {
								String username = u1.getUsername();
								int id = u1.getUser_id();%>
						<option value="<%=id%>"><%=username%></option>
						<%}
							}
						}
						if (ulist.size() == 0) out.println("Nie ma zadnych uzytkownikow w bazie");
						
						s.getTransaction().commit();%>
						</select>
				</td>
			</tr>
			<tr>
			<td></td>
			<td>
			<input type="submit" class="mainBtn" value="Wyślij zaproszenie" />
			</td>
			</tr>
			</table>
			</form>


</div>

<br/><br />
<div id="friendDetails">
<div id="frnd"></div>
</div>